<template>
  <!--     @click.stop="
      () => {
        !layoutCustom ? rowClickHandler(data) : '';
      }
    " -->
  <div
    class="wik-table-row-wrapper"
    :class="{ 'wik-table-row-setting': minWidth }"
    v-show="!data.isShow"
    v-prevent-click-on-copy="{
      handler: () => {
        !layoutCustom ? rowClickHandler(data) : '';
      },
      autoClear: false
    }"
  >
    <div
      class="wik-table-row"
      :class="{ 'wik-table-row-hover': !layoutCustom }"
      :style="`grid-template-columns: ${
        layoutCustom
          ? '1fr'
          : columns.map((item: TableColumn) => widthLength(item.width, minWidth)).join(' ') + ' ' + settingStyle
      }`"
    >
      <!-- 内容定制行 -->
      <div class="wik-table-row-item wik-table-row-item-first" v-if="layoutCustom">
        <div class="wik-table-row-item-first-left">
          <div
            class="wik-table-selection"
            :style="` border-right: 1px solid #eeeeee;`"
            v-if="columns.some((item: TableColumn) => item.type && item.type === 'selection')"
          >
            <el-checkbox size="default" />
          </div>
          <div
            :style="`box-sizing:border-box;padding-left: ${16}px;`"
            class="column-content column-content-first"
            :class="{ 'column-content-layout': data.children && data.children.length }"
            @click="
              () => {
                data.children && data.children.length ? expandFn(data) : '';
              }
            "
          >
            <div class="column-content-item">
              <span
                :class="{ 'column-content-icon-active': !data.isExpand }"
                class="column-content-icon"
                v-if="data.children && data.children.length"
              >
                <ArrowDown />
              </span>
              <span>{{ data[columns[1].key] }}</span>
              <slot></slot>
            </div>
          </div>
        </div>
        <slot :name="`customLayout${layoutCustom.layout}`" :scope="data"></slot>
      </div>
      <div
        v-else
        :class="{
          'wik-table-sticky': item.isFixed || excludeFields.includes(item.type),
          'wik-table-row-item_no': item.type === 'selection',
          ...classNameExpand(item.className)
        }"
        class="wik-table-row-item wik-table-row-item-other"
        v-for="(item, index) in columns"
        :key="index"
        :ref="
          el => {
            setTableColRef(el, data);
          }
        "
      >
        <div class="wik-table-selection" v-if="item.type === 'selection'">
          <el-checkbox size="default" />
        </div>
        <div v-else-if="item.type === 'index'">
          <span v-if="item.width">{{ data.dataIndex }}</span>
        </div>
        <!-- <div :style="index === 1 ? 'width: 100%; overflow: hidden' : 'width: 100%;'" v-else> -->
        <!-- 除了type是selection的列，第一个列如果有children显示展开的icon -->
        <div
          v-else
          class="column-content"
          :class="{
            'column-content-first': index === 1,
            'column-content-layout': data.children && data.children.length && index === 1
          }"
          :style="index === 1 && data.children && data.children.length ? `padding-left: ${layout * 16}px` : ''"
        >
          <div class="column-content-item">
            <span
              class="column-content-icon"
              :class="{ 'column-content-icon-active': !data.isExpand }"
              v-if="data.children && data.children.length && index === 1"
              @click.stop="
                () => {
                  data.children && data.children.length ? expandFn(data) : '';
                }
              "
            >
              <ArrowDown />
            </span>
            <span class="item-content" v-if="!slots[item.key]">{{ data[item.key] }}</span>
            <slot v-else :name="`${item.key}`" :scope="data"></slot>
          </div>
        </div>
        <!-- </div> -->
      </div>
      <!-- 空余一行-->
      <div class="wik-table-row-item" v-if="setting.show"></div>
    </div>
    <div v-if="data.children && data.children.length">
      <table-row
        :data="item"
        v-for="(item, index) in data.children"
        :key="`${item.id}_${index}_table`"
        :layout="layout + 1"
        :serial-number="index"
        @row-click="rowClickHandler"
      >
        <template v-for="slot in Object.keys(slots)" #[slot]="item">
          <slot :name="slot" v-bind="item"></slot>
        </template>
      </table-row>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { inject, ref, onMounted, watch, computed, useSlots, unref } from "vue";
import { ArrowDown } from "@element-plus/icons-vue";
import { useData } from "@table/hooks/useData";
import { useUtils } from "@table/hooks/useUtils";
import { TableColumn } from "../interface";
import { widthLength, classNameExpand } from "../utils";
import { excludeFields } from "@table/constant";
const { tableColumns: columns } = useData();
const options = inject("options", ref({}));
const customContentLayout = inject("customContentLayout", ref([]));

const { vPreventClickOnCopy } = useUtils();
// props
const props = defineProps({
  data: { type: Array, default: () => [] },
  layout: { type: Number, default: 0 },
  serialNumber: { type: Number, default: 0 }
});
const { setting, minWidth } = unref(options);
// definedEmits
const emits = defineEmits(["rowClick"]);
// hook
const slots = useSlots();
// 变量
let expandIndex = ref(0);
let tableColRef = ref<HTMLElement[]>([]);
// computed

const layoutCustom = computed(() => {
  return customContentLayout.find(item => item.layout === props.layout);
});

const settingStyle = computed(() => {
  return setting.show ? `minmax(${widthLength(undefined, minWidth)}, 1fr)` : "";
});
// watch
watch(
  () => columns.value,
  newVal => {
    expandIndex.value = newVal.reduce((pre, cur) => {
      if (["selection"].includes(cur.type)) {
        ++pre;
      }
      return pre;
    }, 0);
  }
);
// 方法
const rowClickHandler = val => {
  emits("rowClick", val);
};
const recursionFn = (data, value, cb) => {
  data.forEach(item => {
    if (item.children && item.children.length) {
      item.isExpand = value;
      recursionFn(item.children, value, cb);
    }
    cb(item, value);
  });
};
const expandFn = data => {
  data.isExpand = !data.isExpand;
  recursionFn(data.children, data.isExpand, item => {
    item.isShow = data.isExpand;
  });
  console.log(data);
};
const setTableColRef = (el, data) => {
  if (el) {
    tableColRef.value.push(el);
    data.tableColRef = tableColRef.value;
    data.serialNumber = props.serialNumber;
  }
};
onMounted(() => {});
</script>
<style lang="scss" scoped>
@use "../styles/index" as *;
.wik-table-row-wrapper {
  width: 100%;
}
.wik-table-row-setting {
  min-width: max-content;
}
.wik-table-row {
  box-sizing: border-box;
  display: grid;
  min-height: 45px;
  text-align: center;
  .wik-table-row-item {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    height: 100%;
    font-size: 14px;
    font-weight: 400;
    color: var(--el-text-color-regular);
    text-align: left;
    border: 1px solid #eeeeee;
    border-top: 0;
    border-right: 0;
    &:last-child {
      border-right: 1px solid #eeeeee;
    }
    .column-content-layout {
      cursor: pointer;
      &:hover {
        color: $primary-color;
      }
    }
    .column-content {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: inherit;
      .column-content-item {
        display: flex;
        align-items: center;
        width: 100%;
        height: inherit;
        word-break: break-all;
        .item-content {
          width: 100%;
          word-break: break-all;
        }
      }
      .column-content-icon {
        width: 16px;
        margin-right: 10px;
        font-size: 16px;
        cursor: pointer;
        transition: all 0.3s;
        transform: rotate(-90deg);
      }
      .column-content-icon-active {
        transform: rotate(0deg);
      }
    }
    .column-content-first {
      justify-content: flex-start;
      text-align: left;
    }
  }
  .wik-table-row-item_no {
    padding: 0;
  }
  .wik-table-row-item-first {
    display: flex;
    justify-content: space-between;
    border: 0;
    border-bottom: 1px solid #eeeeee;
    .wik-table-row-item-first-left {
      display: flex;
    }
  }
  .wik-table-row-item-other {
    padding: 6px 10px;
    &:hover {
      background: $wik-table-column-color;
    }
  }
}
.wik-table-row-hover {
  // min-width: max-content;
  &:hover {
    background: #fafafa;
  }
}
.wik-table-row-item-padding {
  padding: 6px 10px;
}
</style>
